package helpers;

import java.util.ArrayList;
import java.util.regex.Pattern;

import models.AppShopper;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import debugs.AppShopperDebug;

public class CrawlerAppShopperHelper {
	private static String url = "http://appshopper.com/prices/";
	
	private static String regItem = ".wrapper .content .appdetails li";
	
	private static String regId = "";
	
	private static String regTitle = "h3.hovertip a";
	
	private static String regDesc = "p";
	
	private static String regCategory = ".category";
	
	private static String regPrice = ".widget .price";
	
	private static String regPriceDrop = "div.pricedrop";
	
	private static String regImage = "a img";
	
	private static String regFeatured = "dl dd:eq(1)";
	
	private static String regRating = "dl dd:eq(3)";
	
	private static String regVersion = "dl dd:eq(5)";
	
	public static ArrayList<AppShopper> getApp(String strUrl) {
		url = strUrl;
		ArrayList<AppShopper> apps = new ArrayList<AppShopper>();
		Boolean isBreak = false;
		try {
			for (int page = 1; page <= 10; page++) {
				if (isBreak) break;
				String urlPage = url + page;
				Document doc = Jsoup.connect(urlPage).get();
				Elements items = doc.select(regItem);
				AppShopper appShopper = new AppShopper();
				
				if (items.size() > 0) {
					for (int i = 0; i < items.size(); i++) {
						String id = items.get(i).attr("id").replaceAll("[^0-9]+", "");
						String name = items.get(i).select(regTitle).html();
						String desc = (String) items.get(i).select(regDesc).html();
						String priceDrop = getPriceDrop(items.get(i).select(regPriceDrop).html());
						String price = getPrice(items.get(i).select(regPrice).html());
						String image = (String) items.get(i).select(regImage).attr("src");
						String category = (String) items.get(i).select(regCategory).html();
						String featured = (String) items.get(i).select(regFeatured).html();
						String rating = (String) items.get(i).select(regRating).html();
						String version = (String) items.get(i).select(regVersion).html();
						
						if (featured.contains("day")) {
							isBreak = true;
							break;
						}
						
						appShopper = new AppShopper();
						appShopper.setId(id);
						appShopper.setName(name);
						appShopper.setDescription(desc);
						appShopper.setPriceDrop(priceDrop);
						appShopper.setPrice(price);
						appShopper.setImage(image);
						appShopper.setCategory(category);
						appShopper.setFeatured(featured);
						appShopper.setRating(rating);
						appShopper.setVersion(version);
						
						apps.add(appShopper);
						AppShopperDebug.debug(appShopper);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return apps;
	}
	
	private static String getPrice(String price) {
		price = price.replaceAll("(<a[^<>]+>)+[^<>]+(</a>)+", "").replaceAll("[<]+[^<>]+[>]", "").replaceAll("[^a-zA-Z0-9.$]", "");
		if (!Pattern.matches("^[a-zA-Z]+$", price) && Pattern.matches("^[^.]+$", price)) {
			int len = price.length();
			price = price.substring(0, len-2) + "." + price.substring(len-2, len);
		}
		return price;
	}
	
	private static String getPriceDrop(String priceDrop) {
		priceDrop = priceDrop.replaceAll("[<]+[^<>]+[>]", "").replaceAll("[^a-zA-Z0-9.$]", "");
		if (!Pattern.matches("^[a-zA-Z]+$", priceDrop) && Pattern.matches("^[^.]+$", priceDrop)) {
			int len = priceDrop.length();
			priceDrop = priceDrop.substring(0, len-2) + "." + priceDrop.substring(len-2, len);
		}
		return priceDrop;
	}
	
	public static void main(String[] args) {
		/*String str="<div class=\"abc\">abc</div><a target=\"iTPopUp\" href=\"/link/gallery-of-van-gogh-2\">Buy Now</a>";
		System.out.println(str.replaceAll("(<a[^<>]+>)+[^<>]+(</a>)+", ""));*/
		getApp("http://appshopper.com/iphone/prices/");
	}
}
